package cn.tedu.charging.cost.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;

/**
 * Canal配置类
 * 用于配置阿里巴巴官方的canal.client
 */
@Configuration
public class CanalConfig {

    @Value("${canal.server.host}")
    private String canalServerHost;

    @Value("${canal.server.destination}")
    private String destination;

    /**
     * 创建Canal连接器Bean
     * @return CanalConnector
     */
    @Bean
    public CanalConnector canalConnector() {
        String[] hostPort = canalServerHost.split(":");
        String host = hostPort[0];
        int port = Integer.parseInt(hostPort[1]);

        return CanalConnectors.newSingleConnector(
                new InetSocketAddress(host, port),
                destination,
                "",
                ""
        );
    }
}